package com.example.book.controller;

import com.example.book.model.BookInfo;
import com.example.book.model.PageInfo;
import com.example.book.model.PageResult;
import com.example.book.model.Result;
import com.example.book.service.BookService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {
    private static final Logger log = LoggerFactory.getLogger(BookController.class);

    @Autowired
    private BookService bookService;
    //获取图书列表
    @RequestMapping("/getBookListByPage")
    public PageResult<BookInfo> getBookListByPage (PageInfo pageInfo) {
        log.info("getBookListByPage,{}", pageInfo);
        return bookService.getBookListByPage(pageInfo);
    }
    //添加图书
    @RequestMapping(value = "/addBook",produces = "application/json")
    public Result addBook(BookInfo bookInfo){
        //校验参数
        log.info("添加图书，接收到参数：bookInfo{}",bookInfo);
        if(!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus() == null){
            return Result.fail("非法输入");
        }
        //添加图书
        try{
            Integer result = bookService.addBook(bookInfo);
            if(result > 0){
                return Result.success("添加成功");
            }
        }catch (Exception e) {
            log.error("添加图书异常,e: ", e);
            return Result.fail("内部错误,添加图书失败");
        }
        return Result.fail("内部错误,添加失败");
    }
    //根据id查找图书
    @RequestMapping("/selectBookById")
    public BookInfo selectBookById(Integer id){
        log.info("selectBookById,id:{}", id);
        BookInfo bookInfo = bookService.selectBookById(id);
        System.out.println(bookInfo);
        return bookInfo;
    }
    //根据id更新图书
    @RequestMapping(value = "/updateBookById",produces = "application/json")
    public Result updateBookById(BookInfo bookInfo){
        log.info("updateBookById,bookInfo:{}", bookInfo);
        try {
            Integer ret = bookService.updateBookById(bookInfo);
            if(ret > 0){
                return Result.success("更新图书成功");
            }
        }catch (Exception e) {
            log.error("updateBookById,e: ", e);
            return Result.fail("内部错误,更新图书失败");
        }
        return Result.fail("内部错误,更新图书失败");
    }
    //根据id删除图书
    @RequestMapping(value = "/deleteBookById",produces = "application/json")
    public Result deleteBookById(Integer id){
        log.info("deleteBookById,id:{}", id);
        try {
            Integer ret = bookService.deleteBookById(id);
            if(ret > 0){
                return Result.success("删除图书成功");
            }
        }catch (Exception e) {
            log.error("updateBookById,e: ", e);
            return Result.fail("内部错误,删除图书失败");
        }
        return Result.fail("内部错误,删除图书失败");
    }
    //
    @RequestMapping(value = "/batchDeleteByID",produces = "application/json")
    public Result batchDeleteByID(@RequestParam List<Integer> ids){
        log.info("batchDeleteByID,ids:{}", ids);
        try{
            Integer ret =  bookService.batchDeleteByID(ids);
            if(ret > 0){
                return Result.success("批量删除成功");
            }
        }catch (Exception e) {
            log.error("batchDeleteByID,e: ", e);
            return Result.fail("批量删除失败");
        }
        return Result.fail("批量删除失败");
    }
}
